import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    listStatus: false, //宫格为false 列表为ture  
    hasNextPage: false,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    historyList: wx.getStorageSync('historyItem') || [],
    list: [],
    pageNo: 1,
    tabId: 1,
    sortType: 1,
    stoSortType: 1,
    keyword: ''
  },

  onShow: function () {
    this.getlist();
  },


  //筛选
  handleFilt: function (e) {
    var sortType = e.currentTarget.dataset.type;
    this.setData({
      sortType
    })
    this.getlist()
  },
 

  //跳转详情页
  toGoodsDetail(e) {
    var id = e.currentTarget.dataset.id
    console.log(e)
    wx: wx.navigateTo({
      url: '/pages/good/detail/index?id=' + id,
    })
  },

   

  //价格筛选
  handlePice() {
    var sortType = this.data.sortType;
    if (sortType == 4) {
      sortType = 5
    } else {
      sortType = 4
    }
    console.log(sortType, 'sortType')
    this.setData({
      sortType
    })
    this.getlist()
  },

  handletype() {
    this.setData({
      listStatus: !this.data.listStatus
    })
  },



  //查询列表
  getlist: function () {
    this.goodsRequest()
  },


  //商品请求
  goodsRequest: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/gds/goods/list?keyword=' + this.data.keyword + '&sortType=' + this.data.sortType + '&pageNo=' + this.data.pageNo,
      method: 'POST',
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });
    })
  },
 

  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    var historyList = this.data.historyList || [],
      str = JSON.stringify(historyList),
      val = e.detail.value,
      historyItem = historyList
    if (str.indexOf(val) < 0) {
      historyItem = historyList.concat(val)
      wx.setStorageSync('historyItem', historyItem)
    }

    this.setData({
      historyList: historyItem,
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },
 

})